package com.iap.admin.mediator
{
	import com.iap.admin.event.TagEvent;
	import com.iap.admin.event.VectorEvent;
	import com.iap.admin.event.VectorTagEvent;
	import com.iap.admin.signal.ImageRequestSignal;
	import com.iap.admin.signal.VectorRequestSignal;
	import com.iap.admin.signal.VectorTagRequestSignal;
	import com.iap.admin.view.VectorView;
	import com.iap.admin.view.vector.VectorEditView;
	import com.iap.admin.view.vector.code.VectorEditViewCode;
	import com.iap.app.AppGlobal;
	import com.iap.app.StatusCode;
	import com.iap.app.data.AppData;
	import com.iap.app.model.ServiceResponse;
	import com.iap.common.mediator.BaseMediator;
	import com.iap.common.model.Image;
	import com.iap.common.model.Tag;
	import com.iap.common.model.VectorImage;
	import com.iap.common.model.VectorModel;
	import com.iap.common.model.VectorTag;
	import com.iap.common.signal.TagRequestSignal;
	import com.iap.common.translator.TagTranslator;
	import com.iap.common.translator.VectorTagTranslator;
	
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import spark.components.Group;

	public class VectorEditMediator extends BaseMediator
	{
		[Inject]
		public var view:VectorEditView;
		[Inject]
		public var request:VectorRequestSignal;
		[Inject]
		public var imageRequest:ImageRequestSignal;
		[Inject]
		public var tagRequest:TagRequestSignal;
		[Inject]
		public var vectorTagRequest:VectorTagRequestSignal;

		override public function onRegister():void
		{
			view.onCancel.add(onCancel);
			view.onSubmit.add(onSubmit);
			view.onVectorTagRequest.add(onVectorTagRequest);
			addContextListener(VectorEvent.RESPONSE, responseHandler);
			addContextListener(TagEvent.RESPONSE, tagResponseHandler);
			addContextListener(VectorTagEvent.RESPONSE, vectorTagResponseHandler);
			if(view.imagecontrol != null)
			{
				view.imagecontrol.onUpdate.add(updateVectorImageHandler);
				view.imagecontrol.onImageAdded.add(addVectorImageHandler);
				tagRequest.dispatch(new Tag(), TagRequestSignal.GET);
			}
		}
		
		private function onCancel(itemName:String):void
		{
			switch(itemName)
			{
				case VectorEditViewCode.CANCEL_NAME:
				{
					optionBack();
					break;
				}
			}
		}
		
		private function optionBack():void
		{
			setView(view.parent as Group, new VectorView(), FX_GLIDE_OUT);
		}
		
		private function onSubmit(model:VectorModel, task:String):void
		{
			AppGlobal.startWait();
			request.dispatch(model, task);
		}

		private function onVectorTagRequest(model:VectorTag, task:String):void
		{
			AppGlobal.startWait();
			vectorTagRequest.dispatch(model, task);
		}
		
		private function responseHandler(evt:VectorEvent):void
		{
			AppGlobal.endWait();
			var response:ServiceResponse = evt.response;
			switch(response.task)
			{
				case VectorRequestSignal.DELETE:
				{
					optionBack();
					break;
				}
				case VectorRequestSignal.SAVE:
				{
					view.fill(response.result as VectorModel);
					break;
				}
				case VectorRequestSignal.VALID_REMOVE:
				{
					var needConfirmation:Boolean = Boolean(response.rawdata);
					view.removeVector(!needConfirmation);
					break;
				}
			}
		}

		private function onImageRequest(model:Image, task:String):void
		{
			AppGlobal.startWait();
			imageRequest.dispatch(model, task);
		}
		
		private function updateVectorImageHandler(data:Array, task:String):void
		{
			if(task == "sorted")
			{
				view.submit();
			} else {
				view.preview.setSource(data);
			}
		}

		private function addVectorImageHandler(task:String):void
		{
			if(task == "imageAdded")
				view.submit();
		}

		private function tagResponseHandler(evt:TagEvent):void
		{
			AppGlobal.endWait();
			view.tagData = new ArrayCollection(TagTranslator.translateFromArray(evt.response.result as Array));
		}
		
		private function vectorTagResponseHandler(evt:VectorTagEvent):void
		{
			AppGlobal.endWait();
			view.setVectorTagResponse(evt.response);
		}
	}
}